package cn.edu.scau.cmi.yangminghao.client;

import java.util.ArrayList;

import cn.edu.scau.cmi.yangminghao.composite.Componet;
import cn.edu.scau.cmi.yangminghao.composite.Man;
import cn.edu.scau.cmi.yangminghao.composite.Organize;
import cn.edu.scau.cmi.yangminghao.composite.Women;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
/**
 * 组合模式
 * @author Feng
 *
 */
public class interProgrammingClient extends Application{
	public static String org = "";
	static ComboBox<String> cbo4 = new ComboBox<String>();
	static ArrayList<Componet> lis = new ArrayList<Componet>();
	static Organize s1 = new Organize("实验高中");
	static Organize s2 = new Organize("高二");
	static Organize s3 = new Organize("高二二班");
	static Organize s4 = new Organize("高二一班");	
	static Organize s5 = new Organize("高三");
	static Organize s6 = new Organize("高三一班");
	static Organize s7= new Organize("高三二班");
	
	public static void main(String[] args) {
		s1.add(s2);
		s1.add(s5);
		s2.add(s4);
		s2.add(s3);
		s5.add(s6);
		s5.add(s7);
		Application.launch(args);		
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		HBox hbox1 = new HBox(10);
		hbox1.setPadding(new Insets(20,0,50,30));
		ComboBox<String> cbo1 = new ComboBox<>();
		cbo1.getItems().addAll("男", "女");
		cbo1.setValue("男");
		ComboBox<String> cbo2 = new ComboBox<>();
		cbo2.getItems().addAll("高二一班", "高二二班","高三一班","高三二班");
		cbo2.setValue("高二一班");
		TextField textfield = new TextField();
		Button button1 =new Button("创建学生");
		hbox1.getChildren().addAll(new Label("性别:"),cbo1,new Label("姓名:"),textfield,new Label("所属班级:"),cbo2,button1);
		
		button1.setOnAction(e->{
			Componet t ;
			if(cbo2.getValue().equals("高二一班")&&!textfield.getText().equals("")) {
				if(cbo1.getValue().equals("男")) {
					t=new Man(textfield.getText().toString());
					lis.add(t);
					s4.add(t);
				}
				else {
					t=new Women(textfield.getText().toString());
					lis.add(t);
					s4.add(t);
					
				}
				check();
				create();
			}
			else if(cbo2.getValue().equals("高二二班")&&!textfield.getText().equals("")) {
				if(cbo1.getValue().equals("男")) {
					t=new Man(textfield.getText().toString());
					lis.add(t);
					s3.add(t);
				}
				else {
					t=new Women(textfield.getText().toString());
					lis.add(t);
					s3.add(t);
				}
				check();
				create();
			}
			else if(cbo2.getValue().equals("高三一班")&&!textfield.getText().equals("")) {
				if(cbo1.getValue().equals("男")) {
					t=new Man(textfield.getText().toString());
					lis.add(t);
					s6.add(t);
				}
				else {
					t=new Women(textfield.getText().toString());
					lis.add(t);
					s6.add(new Women(textfield.getText().toString()));
				}
				check();
				create();
			}
			else if(cbo2.getValue().equals("高三二班")&&!textfield.getText().equals("")) {
				if(cbo1.getValue().equals("男")) {
					t=new Man(textfield.getText().toString());
					lis.add(t);
					s7.add(t);
				}
				else {
					t=new Women(textfield.getText().toString());
					lis.add(t);
					s7.add(t);
				}
				check();
				create();
			}
			else {
				Alert warning = new Alert(Alert.AlertType.WARNING,"创建失败,输入格式有误");
				Button warn = new Button();		
				warning.showAndWait();
			}
			
		});
		HBox hbox2 = new HBox(15);
		hbox2.setPadding(new Insets(20,0,20,180));
		ComboBox<String> cbo3 = new ComboBox<>();
		cbo3.getItems().addAll("实验高中","高二","高三","高二一班", "高二二班","高三一班","高三二班");
		cbo3.setValue("实验高中");
		Button button2 = new Button("查询");
		hbox2.getChildren().addAll(new Label("选择组织:"),cbo3,button2);
		

		
		Label label = new Label("      创建");
		label.setFont(Font.font("Timer New Roman",
		FontWeight.BOLD, FontPosture.ITALIC, 25));
		
		Label label2 = new Label("      查询");
		label2.setFont(Font.font("Timer New Roman",
		FontWeight.BOLD, FontPosture.ITALIC, 25));
		
		Label label3 = new Label("      删除");
		label3.setFont(Font.font("Timer New Roman",
		FontWeight.BOLD, FontPosture.ITALIC, 25));
		
		
		TextArea textarea = new TextArea();
		textarea.setEditable(false);
		textarea.setPrefRowCount(20);
		
		button2.setOnAction(e->{
			org="";
			if(cbo3.getValue().equals("实验高中")) {
				s1.name(1);
			}
			else if(cbo3.getValue().equals("高二")) {
				s2.name(1);
			}
			else if(cbo3.getValue().equals("高三")) {
				s5.name(1);
			}
			else if(cbo3.getValue().equals("高三一班")) {
				s6.name(1);
			}
			else if(cbo3.getValue().equals("高三二班")) {
				s7.name(1);
			}
			else if(cbo3.getValue().equals("高二一班")) {
				s4.name(1);
			}
			else if(cbo3.getValue().equals("高二二班")) {
				s3.name(1);
			}
			textarea.setText(org);
		});
		
		HBox hbox3 = new HBox(20);
		hbox3.setPadding(new Insets(20,0,50,220));
		Button button3 = new Button("删除");
		
		
		
		
		hbox3.getChildren().addAll(cbo4,button3);
		button3.setOnAction(e->{
			for(Componet ss :lis) {
				if(ss.getNames().equals(cbo4.getValue().toString())) {
					s1.remove(ss);
					s2.remove(ss);
					s3.remove(ss);
					s4.remove(ss);
					s5.remove(ss);
					s6.remove(ss);
					s7.remove(ss);
					lis.remove(ss);
					
					break;
				}
			}
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setTitle("删除");
			alert.setHeaderText(null);	
			alert.setContentText("成功删除");
			alert.showAndWait();	
			check();
		});
		
		
		
		VBox vbox = new VBox();
		vbox.getChildren().addAll(label,hbox1,label3,hbox3,label2,hbox2,textarea);
		Scene scene = new Scene(vbox,600,700);
		primaryStage.setTitle("实验五：学校班级名单");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	public static void create() {
		Alert alert = new Alert(AlertType.INFORMATION);
		alert.setTitle("创建");
		alert.setHeaderText(null);	
		alert.setContentText("成功创建！");
		alert.showAndWait();	
	}
	public static void check() {
		cbo4.getItems().clear();
		for(Componet ss:lis) {
			cbo4.getItems().add(ss.getNames());
		}
		
	}
}
